<template>
   <el-container class="home_container">
      <el-header>
         <div>
            <img src="../assets/heima.png" alt="">
            <span>黑马电商管理后台</span>
         </div>
         <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
         <!-- 侧边栏 -->
         <el-aside :width="iscollapse?'64px':'200px'">
            <div class="toggle-button" @click="btn">|||</div>
            <el-menu background-color="#313743" text-color="#fff" active-text-color="#379bff" unique-opened
               :collapse="iscollapse" :collapse-transition="true" :router="true" :default-active="navactive">
               <!-- 一级菜单 -->
               <el-submenu :index="obj.id+''" v-for="obj in MuneList" :key="obj.id">
                  <!-- 一级菜单的模板区 -->
                  <template slot="title">
                     <i :class="iconsObj[obj.id]"></i>
                     <span>{{obj.authName}}</span>
                  </template>
                  <!-- 二级菜单的模板区 -->
                  <el-menu-item :index="'/'+item.path" v-for="item in obj.children" :key="item.id"
                     @click="saveNavActive('/'+item.path)">
                     <template slot="title">
                        <i class="el-icon-menu"></i>
                        <span>{{item.authName}}</span>
                     </template>
                  </el-menu-item>
               </el-submenu>
            </el-menu>
         </el-aside>
         <el-main>
            <router-view></router-view>
         </el-main>
      </el-container>
   </el-container>
</template>

<script>
import { homeNavApi,  } from '@/api/index'
export default {
   data() {
      return {
         MuneList: [],
         iconsObj: {
            '125': 'iconfont icon-user',
            '103': 'iconfont icon-tijikongjian',
            '101': 'iconfont icon-shangpin',
            '102': 'iconfont icon-danju',
            '145': 'iconfont icon-baobiao',
         },
         iscollapse: false,
         navactive: '',

      }
   },
   created() {
      this.getMuneList()
      this.navactive = window.sessionStorage.getItem('active')
   },
   methods: {
      logout() {
         window.sessionStorage.clear()
         this.$router.push('/login')
      },
      async getMuneList() {
         const { data: res } = await homeNavApi()
         if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
         this.MuneList = res.data
         //  console.log(this.MuneList)
      },
      btn() {
         this.iscollapse = !this.iscollapse
      },
      saveNavActive(val) {
         window.sessionStorage.setItem('active', val)
         this.navactive = val
      },
   }
}
</script>

<style >
.el-header {
   background-color: #363d40;
   display: flex;
   justify-content: space-between;
   align-items: center;
   padding-left: 0px;
   color: #fff;
   font-size: 20px;
}

.el-header div {
   display: flex;
   justify-content: center;
   align-items: center;
}

.el-header div span {
   padding-left: 20px;
}

.el-aside {
   background-color: #313743
}

.el-main {
   background-color: #e9edf0
}

.home_container {
   height: 100%;
}

.iconfont {
   margin-right: 15px;
}

.el-menu {
   border-right: 0;
}

.toggle-button {
   background-color: #4a5064;
   font-size: 10px;
   line-height: 24px;
   color: #fff;
   text-align: center;
   letter-spacing: 0.2em;
   cursor: pointer;
}
</style>